;;;=================================================================
;;;
;;; Subject: Pair format to natural format
;;;
;;;-----------------------------------------------------------------
;;;
;;; Jónathan Heras Vicente
;;; Beginning date: 26-04-10
;;;
;;;=================================================================


(defvar *pair-natural* (make-hash-table :test 'equal))

(defvar *counter* 0)

(defun transform-cons-pair (cons-pair)
  (cond ((endp cons-pair) nil)
        (t (let ((val (gethash (car cons-pair) *pair-natural*)))
             (if val
                 (cons val (transform-cons-pair (cdr cons-pair)))
               (progn (setf (gethash (car cons-pair) *pair-natural*) (1+ *counter*))
                 (cons (incf *counter*) (transform-cons-pair (cdr cons-pair)))))))))


(defun transform-list-of-cons-pair-aux (list)
  (cond ((endp list) nil)
        (t (cons (transform-cons-pair (car list)) 
                 (transform-list-of-cons-pair-aux (cdr list))))))


(defun flatten (lol)
  (if (endp lol)
      nil
    (append (car lol) (flatten (cdr lol)))))
  
  
(defun l-cmpr-fun (list1 list2)
  (if (equal (l-cmpr list1 list2) :less)
      t
    nil))




(defun transform-list-of-cons-pair (list)
  (progn
    (clrhash *pair-natural*)
    (setf *counter* 0)
    (transform-list-of-cons-pair-aux list)))


(defun store-vertices (list)
  (cond ((endp list) nil)
        (t (progn 
             (setf (gethash (car list) *pair-natural*) (1+ *counter*))
             (incf *counter*)
             (store-vertices (cdr list))))))


(defun transform-lolol-to-lol (list)
  (progn
    (clrhash *pair-natural*)
    (setf *counter* -1)
    (store-vertices 
     (sort (remove-duplicates (flatten list) :test #'equal) #'l-cmpr-fun)
     )
    (transform-list-of-cons-pair-aux list)
    ))


#|

Example:

(transform-list-of-cons-pair (list-to-facets '((1 0) (0 1))))

|#